package com.wn.ziroom.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MqConfiguration {


    private static final String NORMAL_QUEUE = "NORMAL_QUEUE";

    private static final String DEAD_QUEUE = "DEAD_QUEUE";

    private static final String NORMAL_EXCHANGE = "NORMAL_EXCHANGE";

    private static final String DEAD_EXCHANGE = "DEAD_EXCHANGE";

    private static final String NORMAL_ROUTING = "NORMAL_ROUTING";

    private static final String DEAD_ROUTING = "DEAD_ROUTING";

    @Bean(name = NORMAL_QUEUE)
    public Queue normalQueue(){
        Map<String,Object> params = new HashMap<>();
        //-- 正常队列到死信交换机的设置
        params.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        params.put("x-dead-letter-routing-key",DEAD_ROUTING);
        params.put("x-message-ttl",1000*60*1);
        return QueueBuilder.durable(NORMAL_QUEUE).withArguments(params).build();
    }

    @Bean(DEAD_QUEUE)
    public Queue deadQueue(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    @Bean(NORMAL_EXCHANGE)
    public DirectExchange normalExchange(){
        return ExchangeBuilder.directExchange(NORMAL_EXCHANGE).durable(true).build();
    }

    @Bean(DEAD_EXCHANGE)
    public DirectExchange deadExchange(){
        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).durable(true).build();
    }

    @Bean
    public Binding normalBinding(@Qualifier(NORMAL_QUEUE) Queue normalQueue,@Qualifier(NORMAL_EXCHANGE) DirectExchange normalExchange){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with(NORMAL_ROUTING);
    }

    @Bean
    public Binding deadBinding(@Qualifier(DEAD_QUEUE) Queue deadQueue,@Qualifier(DEAD_EXCHANGE) DirectExchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING);
    }

}
